/**
 * 控制器中的函数声明以及一些结构体
 * controller.h
 */

#ifndef __CONTROLLER_H__
#define __CONTROLLER_H__ 


#include "module.h"
#include "functions.h"


typedef struct temp_club_ranking
{
	char clubname[CLUBNAME_LEN];
	char coach[PERSON_LEN];
	int sitepoint;
	int gamepoint;
	int win;
	int lose;
	struct temp_club_ranking *next;
}Tcr;

typedef struct temp_player_ranking
{
	char player[PERSON_LEN];
	char clubname[CLUBNAME_LEN];
	int gamesum;
	int win;
	int lose;
	float rate;
	struct temp_player_ranking *next;
}Tpr;

typedef struct temp_rank_ranking
{
	char clubname[CLUBNAME_LEN];
	char name[PERSON_LEN];
	char rank;
	int gamesum;
	int victories;
	struct temp_rank_ranking *next;
}Trr;

void app_run();

void run_load(GtkWidget *widget, gpointer data);
void run_save(GtkWidget *widget, gpointer data);
void run_about_dialog(GtkWidget *widget, gpointer data);

void run_insert_club(GtkWidget *widget, gpointer data);
int run_insert_club_with_input(int *flag);
void run_insert_player(GtkWidget *widget, gpointer data);
int run_insert_player_with_input(int *flag);
void run_insert_game(GtkWidget *widget, gpointer data);
int run_insert_game_with_input(int *flag);
void call_player_data_black(GtkComboBoxText *widget, GObject *combo, gpointer data);
void call_player_data_white(GtkComboBoxText *widget, GObject *combo, gpointer data);
void call_player_data(GtkComboBoxText *combo_from, GtkComboBoxText *combo_to);

static void fill_data_query_club(const char *data, char mode);
void run_query_club_by_substring(GtkWidget *widget, gpointer data);
int run_query_club_by_substring_loop(int *flag);
void run_query_club_by_coach(GtkWidget *widget, gpointer data);
int run_query_club_by_coach_loop(int *flag);

static void fill_data_query_player(const char *data, const int v, char mode);
void run_query_player_by_name(GtkWidget *widget, gpointer data);
int run_query_player_by_name_loop(int *flag);
void run_query_player_by_club_and_victories(GtkWidget *widget, gpointer data);
int run_query_player_by_club_and_victories_loop(int *flag);
void run_query_player_by_clubname(GtkWidget *widget, gpointer data);
static int run_query_player_by_clubname_loop(int *flag);

static void fill_data_query_game(const char *data, const char *clubname, const int arg, char mode);
void run_query_game_by_id_and_player(GtkWidget *widget, gpointer data);
int run_query_game_by_id_and_player_loop(int *flag);
void run_query_game_by_player_and_status(GtkWidget *widget, gpointer data);
int run_query_game_by_player_and_status_loop(int *flag);
void run_query_game_by_clubname_and_player(GtkWidget *widget, gpointer data);
static int run_query_game_by_clubname_and_player_loop(int *flag);

void run_update_coach(GtkWidget *widget, gpointer data);
int run_update_coach_loop(int *flag);
void run_update_rank(GtkWidget *widget, gpointer data);
int run_update_rank_loop(int *flag);
static void call_player_data_for_update(GtkWidget *widget, gpointer data);
void run_update_game(GtkWidget *widget, gpointer data);
static void insert_game_for_update();
static int run_update_game_loop(int *flag);
static void call_player_data_for_update_black(GtkWidget *widget, gpointer data);
static void call_player_data_for_update_white(GtkWidget *widget, gpointer data);

void run_delete_club(GtkWidget *widget, gpointer data);
static int run_delete_club_loop(int *flag);
void run_delete_player(GtkWidget *widget, gpointer data);
static int run_delete_player_loop(int *flag);
void run_delete_game(GtkWidget *widget, gpointer data);
static int run_delete_game_loop(int *flag);

static int check_before_ranking();
static void run_warning_dialog();
void run_club_ranking(GtkWidget *widget, gpointer data);
static void club_ranking(Tcr **pt_head);
void run_player_ranking(GtkWidget *widget, gpointer data);
static void player_ranking(Tpr **pt_head);
void run_rank_sum(GtkWidget *widget, gpointer data);
static void rank_sum(int (*sum)[9]);
void run_club_info(GtkWidget *widget, gpointer data);
void run_rank_ranking(GtkWidget *widget, gpointer data);
static void rank_ranking(Trr **pt_head);

void run_show_clubs(GtkWidget *widget, gpointer data);
void run_show_all(GtkWidget *widget, gpointer data);
gboolean run_quit_dialog(GtkWidget *widget, 
						 GdkEvent *event,
						 gpointer data);


#endif	/* __CONTROLLER_H__ */